"use strict";

const {Service} = require("egg");
class tagService extends Service{
  async getAll(){
    const {mysql,logger} = this.app
    try {
      let query = await mysql.select("tag")
      return {suc:true,msg:"查询成功",data:query}
    }catch (e){
      logger.error(e)
      return {suc:false,msg:e}
    }
  }

  async getByQuestion(q_id){
    const {mysql,logger} = this.app
    const result = []
    try {
      /** @type{QuestionTag[]}*/
      let queryQuestionTag = await mysql.select("q_tag",{where:{q_id:q_id}})
      //console.log(queryQuestionTag)
      for(let i in queryQuestionTag){
        /**@type{Tag} */
        let tag = await mysql.get('tag',{id:queryQuestionTag[i].t_id})
        if(tag!=null){
          result.push(tag)
        }
      }
      return {suc:true,msg:"查询成功",data:result}
    }catch (e){
      logger.error(e)
      return {suc:false,msg:e}
    }
  }
}

module.exports = tagService